package com.ruoyi.ewem.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ewem.domain.Drivers;
import com.ruoyi.ewem.domain.Ininfo;
import com.ruoyi.ewem.domain.Physical;
import com.ruoyi.ewem.domain.Warehouse;
import com.ruoyi.ewem.service.IEwemDriversService;
import com.ruoyi.ewem.service.IEwemPhysicalService;
import com.ruoyi.ewem.service.IninfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 物流运输Controller
 * 
 * @author ruoyi
 * @date 2024-07-27
 */
@RestController
@RequestMapping("/physical")
public class EwemPhysicalController extends BaseController
{
    @Autowired
    private IEwemPhysicalService ewemPhysicalService;

    @Autowired
    IEwemDriversService driversService;

    @Autowired
    private IninfoService ininfoService;

    /**
     * 查询物流运输列表
     */
    @RequiresPermissions("ewem:physical:list")
    @GetMapping("/list")
    public TableDataInfo list(Physical ewemPhysical)
    {
        startPage();
        List<Physical> list = ewemPhysicalService.selectEwemPhysicalList(ewemPhysical);
        return getDataTable(list);
    }

    /**
     * 导出物流运输列表
     */
    @RequiresPermissions("ewem:physical:export")
    @Log(title = "物流运输", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Physical ewemPhysical)
    {
        List<Physical> list = ewemPhysicalService.selectEwemPhysicalList(ewemPhysical);
        ExcelUtil<Physical> util = new ExcelUtil<Physical>(Physical.class);
        util.exportExcel(response, list, "物流运输数据");
    }

    /**
     * 获取物流运输详细信息
     */
    @RequiresPermissions("ewem:physical:query")
    @GetMapping(value = "/{physicalId}")
    public AjaxResult getInfo(@PathVariable("physicalId") Long physicalId)
    {
        return success(ewemPhysicalService.selectEwemPhysicalByPhysicalId(physicalId));
    }

    /**
     * 新增物流运输
     */
    @RequiresPermissions("ewem:physical:add")
    @Log(title = "物流运输", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Physical ewemPhysical)
    {
        ewemPhysical.setStatus("0");
        ewemPhysical.setPhysicalNum(IdUtil.getSnowflake().nextIdStr());
        return toAjax(ewemPhysicalService.insertEwemPhysical(ewemPhysical));
    }

    /**
     * 修改物流运输
     */
    @RequiresPermissions("ewem:physical:edit")
    @Log(title = "物流运输", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Physical ewemPhysical)
    {
        return toAjax(ewemPhysicalService.updateEwemPhysical(ewemPhysical));
    }

    /**
     * 删除物流运输
     */
    @RequiresPermissions("ewem:physical:remove")
    @Log(title = "物流运输", businessType = BusinessType.DELETE)
	@DeleteMapping("/{physicalIds}")
    public AjaxResult remove(@PathVariable Long[] physicalIds)
    {
        return toAjax(ewemPhysicalService.deleteEwemPhysicalByPhysicalIds(physicalIds));
    }

    @PostMapping("/ListDrivers")
    public AjaxResult ListDrivers()
    {
       List<Drivers> list = driversService.selectDriversList();
       return AjaxResult.success(list);
    }
    @PostMapping("/ListWarehouse")
    public AjaxResult ListWarehouse()
    {
       List<Warehouse> list = ewemPhysicalService.selectWarehouseList();
       return AjaxResult.success(list);
    }

    @PostMapping("/listByuser")
    public AjaxResult listByuser()
    {
        String username = SecurityUtils.getUsername();
        List<Physical> list = ewemPhysicalService.selectlistByuser(username);
        return AjaxResult.success(list);
    }
    @PostMapping("/updateById/{physicalId}")
    public AjaxResult updateById(@PathVariable Integer physicalId)
    {
        Physical byId = ewemPhysicalService.getById(physicalId);
        byId.setStatus("1");
        ewemPhysicalService.updateById(byId);
        return AjaxResult.success();
    }

    @PostMapping("/updateloadById/{physicalId}")
    public AjaxResult updateloadById(@PathVariable Integer physicalId)
    {
        Physical byId = ewemPhysicalService.getById(physicalId);
        byId.setStatus("2");
        Ininfo ininfo = new Ininfo();
        ininfo.setBatchNum(byId.getBatchNum());
        ininfo.setStatus("0");
        ininfo.setWarehouseId(byId.getWarehouseId());
        ininfo.setPhysicalNum(byId.getPhysicalNum());
        ininfo.setSource(byId.getStartName());
        ininfo.setDestination(byId.getEndName());
        ininfo.setCount(1000);
        ininfoService.save(ininfo);
        ewemPhysicalService.updateById(byId);
        return AjaxResult.success();
    }

    @PostMapping("/listAll")
    public AjaxResult listAll()
    {
        List<Physical> list = ewemPhysicalService.listAll();
        return AjaxResult.success(list);
    }
}
